package com.dshore.messagecenter.dao.warning;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.system.SysDict;

@Repository
public class WarningIndicatorsDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 总记录数
	 * @param sysDict
	 * @return
	 */
	public long queryCountByPrama(SysDict sysDict) {
		StringBuffer sql = new StringBuffer();
		List<Object> list=new ArrayList<>();
		sql.append("select count(1) from T_SYS_DICT where DEL_FLAG=0 and DICT_TYPE='kpi_type'");
		if(StringUtils.isNotBlank(sysDict.getDictName())) {
			sql.append(" and DICT_NAME like ?");
			list.add("%"+sysDict.getDictName()+"%");
		}
		Object[]args=list.toArray();
		return jdbcTemplate.queryForObject(sql.toString(),args,long.class);
	}
	/**
	 * 查询数据
	 * @param sysDict
	 * @return
	 */
	public List<SysDict> queryListByPrama(SysDict sysDict) {
		List<Object> list=new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from (");
		sql.append("select A.*, rownum rn from (select * from T_SYS_DICT  where del_flag = 0 and DICT_TYPE='kpi_type'");
		if(StringUtils.isNotBlank(sysDict.getDictName())) {
			sql.append(" and DICT_NAME like ?");
			list.add("%"+sysDict.getDictName()+"%");
		}
		sql.append(" ) A   )");
		sql.append(" where rn between ? and ? ");
		sql.append(" order by created_time desc ");
		list.add(sysDict.getOffset()+1);
		list.add(sysDict.getOffset()+sysDict.getLimit());
		RowMapper<SysDict> rowMapper = new BeanPropertyRowMapper<>(SysDict.class);
		Object[]args=list.toArray();
	 return jdbcTemplate.query(sql.toString(),args,rowMapper);
	}
	/**
	 * 查询单条数据
	 * @param id
	 * @return
	 */
	public SysDict getById(String id) {
		String sql = "select * from T_SYS_DICT where id=? and DEL_FLAG=0";
		RowMapper<SysDict> rowMapper = new BeanPropertyRowMapper<>(SysDict.class);
		try {
			return jdbcTemplate.queryForObject(sql,rowMapper,id);
		}catch(DataAccessException e) {
			return null;
		}
	}
	/**
	 * 删除
	 * @param id
	 */
	public void deleteByIds(String[] id) {
		String sql="update T_SYS_DICT set del_flag=1,modified_time=?  where id =?";	
		 jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
				ps.setString(2, id[i].toString());
			}			
			@Override
			public int getBatchSize() {
				return id.length;
			}
		
		 });
	}
	/**
	 * 新增
	 * @param sysDict
	 * @return
	 */
	public Integer addIndicators(SysDict sysDict) {
		String sql ="insert into T_SYS_DICT(id,DICT_CODE,DICT_TYPE,"
				+ "DICT_NAME,CREATED_TIME,DEL_FLAG,SEQ) values (?,?,?,?,?,?,?)";
		return jdbcTemplate.update(sql,sysDict.getId(),sysDict.getDictCode(),"kpi_type",
				sysDict.getDictName(),new Date(),0,sysDict.getSeq());
	}
	/**
	 * 修改
	 * @param sysDict
	 * @return
	 */
	public Integer changeIndicators(SysDict sysDict) {
		String sql="update T_SYS_DICT set DICT_NAME=?,MODIFIED_TIME=? where id = ?";		
		return jdbcTemplate.update(sql,sysDict.getDictName(),new Date(),sysDict.getId());
	}
	
	/**
	 * 校验
	 * @param dictName
	 * @return
	 */
	public List<SysDict> checkIndicators(String indicators1) {
		String sql="select ID from T_SYS_DICT  where dict_name=? and del_flag=0 and dict_type='kpi_type'";
		RowMapper<SysDict> rowMapper = new BeanPropertyRowMapper<>(SysDict.class);
		return jdbcTemplate.query(sql, rowMapper,indicators1);
	}
	
	/**
	 * 最大seq
	 * @return
	 */
	public Integer selectMaxSeq() {
		String sql="select max(SEQ) from T_SYS_DICT where dict_type='kpi_type'";
		try {
			return jdbcTemplate.queryForObject(sql,Integer.class);
		}catch(DataAccessException e) {
			return 0;
		}
	}
	/**
	 * 最大code
	 * @param n
	 * @return
	 */
	public String selectMaxCode(Integer n) {
		String sql="select DICT_CODE from T_SYS_DICT where dict_type='kpi_type' and SEQ=?";
		try {
			return jdbcTemplate.queryForObject(sql,String.class,n);
		}catch(DataAccessException e) {
			return null;
		}
	}
	
}
